import 'package:flutter/cupertino.dart';
import 'package:mobile_app.core/core/presentation/theme/theme.dart';
import 'package:mobile_app.core/core/presentation/widgets/app_button.dart';
import 'package:mobile_app.home.core/core/presentation/localization/translation.dart';
import 'package:mobile_app.home.core/core/presentation/theme/home_core_theme.dart';

class ScaledSearchBar extends StatelessWidget {
  const ScaledSearchBar({
    this.focusNode,
    this.controller,
    this.textInputAction,
    this.onChanged,
    this.onSubmitted,
    super.key,
  });

  final FocusNode? focusNode;
  final TextEditingController? controller;
  final TextInputAction? textInputAction;
  final VoidCallback? onChanged;
  final VoidCallback? onSubmitted;

  @override
  Widget build(BuildContext context) {
    final prefix = Padding(
      padding: const EdgeInsets.fromLTRB(6, 0, 0, 2),
      child: Icon(
        CupertinoIcons.search,
        size: 20,
        color: HomeCoreTheme.of(context).searchBarPlaceholderColor,
      ),
    );

    final suffix = Padding(
      padding: const EdgeInsets.fromLTRB(0, 0, 5, 2),
      child: AppButton(
        onPressed: () {
          controller?.clear();
          onChanged?.call();
        },
        child: Icon(
          CupertinoIcons.xmark_circle_fill,
          size: 20,
          color: HomeCoreTheme.of(context).searchBarPlaceholderColor,
        ),
      ),
    );

    return CupertinoTextField(
      focusNode: focusNode,
      controller: controller,
      textAlign: TextAlign.left,
      prefix: prefix,
      suffix: suffix,
      suffixMode: OverlayVisibilityMode.editing,
      style: HomeCoreTheme.of(context).textTheme.searchBar,
      autofocus: false,
      autocorrect: true,
      keyboardType: TextInputType.text,
      keyboardAppearance: CoreTheme.of(context).brightness,
      cursorColor: CoreTheme.of(context).primaryColor,
      textInputAction: textInputAction,
      clearButtonMode: OverlayVisibilityMode.editing,
      padding: const EdgeInsets.fromLTRB(3.8, 8, 5, 8),
      placeholder: HomeCoreTranslation.of(context).searchBarPlaceholder,
      placeholderStyle: HomeCoreTheme.of(context).textTheme.searchBar.copyWith(
            color: HomeCoreTheme.of(context).searchBarPlaceholderColor,
          ),
      decoration: BoxDecoration(
        color: HomeCoreTheme.of(context).searchBarColor,
        borderRadius: BorderRadius.circular(9.0),
      ),
      onChanged: (_) {
        onChanged?.call();
      },
      onSubmitted: (_) {
        onSubmitted?.call();
      },
    );
  }
}
